package generators;

import gamebackend.Block;

import java.util.Random;

public class VerticalScaredGenerator extends AbstractRandomGenerator {

	/**
	 * The chance that there will be no block at the column index.
	 * Decreases when there are empty or path blocks and resets to 1
	 * for filled blocks.
	 */
	private double[] my_missChance;
	public VerticalScaredGenerator(Block the_block, Random the_random,
			double the_chancePerBlock) {
		super(the_block, the_random, the_chancePerBlock);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void generate(Block[][] blocks) {
		if (my_missChance == null) {
			my_missChance = new double[blocks[0].length];
			for (int col = 0; col < blocks[0].length; col++) {
				my_missChance[col] = 1;
			}
		}
		int row = blocks.length - 1;
		for (int col = 0; col < blocks[row].length; col++) {
			if (blocks[row][col].isOccupied()) {
				my_missChance[col] = 1;
			} else {
				my_missChance[col] *= (1 - getChance());
				if (blocks[row][col] == Block.EMPTY_BLOCK
						&& getRandom().nextDouble() > my_missChance[col]) {
					blocks[row][col] = getBlock();
					my_missChance[col] = 1;
				}
			}
		}
		
	}

}
